package hamsterpolice.todo;

import hamsterpolice.todo.model.AuthenticationFailureException;
import hamsterpolice.todo.model.User;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private final String TAG = "LoginActivity";

	private final int NEWUSER_REQUEST = 1;

	private EditText mUserNameEdit = null;
	private EditText mPasswordEdit = null;
	private Button mLoginButton = null;
	private Button mCreateUserButton = null;
	private MediaPlayer mpSplash;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		//Author: Charlie -  creates and starts the sound activity
		mpSplash = MediaPlayer.create(this, R.raw.login);
		mpSplash.start();
		
		mUserNameEdit = (EditText) findViewById(R.id.usernameEdit);
		mPasswordEdit = (EditText) findViewById(R.id.passwordEdit);
		mLoginButton = (Button) findViewById(R.id.loginButton);
		mLoginButton.setOnClickListener(new LoginClickListener(this));
		mCreateUserButton = (Button) findViewById(R.id.registerButton);
		mCreateUserButton.setOnClickListener(new RegisterClickListener(this));
	}

	
	/**
	 * Author: Charlie
	 */
	@Override
	protected void onDestroy(){
		super.onDestroy();
		mpSplash.release();
	}
	/**
	 * Author: Charlie
	 */
	@Override
	protected void onPause(){
		super.onPause();
		mpSplash.pause();
	}
	/**
	 * Author: Charlie
	 */
	@Override
	protected void onResume(){
		super.onResume();
		mpSplash.start();
	}
	/**
	 * Author: Charlie
	 */
	@Override
	protected void onStart(){
		super.onStart();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case NEWUSER_REQUEST:
			switch (resultCode) {
			case Activity.RESULT_OK:
				long userId = data.getLongExtra("USER_ID", -1);
				if (userId != -1) {
					Intent resultIntent = new Intent();
					resultIntent.putExtra("USER_ID", userId);
					setResult(Activity.RESULT_OK, resultIntent);
					finish();
				}
				break;
			}
			break;
		}
	}

	/**
	 * Handler for the Login button.
	 * 
	 * @author Chris
	 * 
	 */
	private class LoginClickListener implements OnClickListener {

		Context mContext;

		public LoginClickListener(Context context) {
			mContext = context;
		}

		@Override
		public void onClick(View v) {

			String username = mUserNameEdit.getText().toString();
			String password = mPasswordEdit.getText().toString();

			try {
				User user = User.Authenticate(mContext, username, password);

				// Return user to calling activity
				Intent resultIntent = new Intent();
				resultIntent.putExtra("USER_ID", user.getID());
				setResult(Activity.RESULT_OK, resultIntent);
				finish();
			} catch (AuthenticationFailureException e) {
				Toast.makeText(mContext, R.string.AUTH_FAILED,
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	/**
	 * Handler for the New User button. Opens the NewUserActivity.
	 * 
	 * @author Chris
	 * 
	 */
	private class RegisterClickListener implements OnClickListener {

		Context mContext;

		public RegisterClickListener(Context context) {
			mContext = context;
		}

		@Override
		public void onClick(View v) {

			try {
				
				Intent registerIntent = new Intent(mContext,
						NewUserActivity.class);
				startActivityForResult(registerIntent, NEWUSER_REQUEST);

				
			} catch (Exception e) {
				Log.e(TAG, "Error opening NewUserActivity", e);
			}
		}
	}
}
